// pages/recommentSong/recommentSong.js
import request from "../../../utils/request";
import PubSub from "pubsub-js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
  userId:null,
  songId:null,
  commentList:[],
  commentContent:null,
    myContentNum:0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let songId = options.musicId
    let userId = options.userId
    this.setData({
      songId:songId,
      userId:userId
    })
    this.getSongComment()
  },

  async getSongComment(){
    let commentList = await request("/song/getSongComment/"+this.data.userId+"/"+this.data.songId)
    this.setData({
      commentList: commentList.data
    })
  },
  handleInput(e){
    this.setData({
      commentContent:e.detail.value
    })
  },
  sendComment(){
    let commentContent = this.data.commentContent;
    let param = {'userId':this.data.userId, 'songId' : this.data.songId,content:commentContent}
    request("/song/sendComment",param,'POST').then(data=>{
      wx.showToast({title:"评论成功"})
      let userInfo = wx.getStorageSync("userInfo")
      let user = JSON.parse(userInfo)
      let commentList = this.data.commentList
      let myContentNum = this.data.myContentNum
      myContentNum = myContentNum+1
      commentList.splice(0,0,
          {content:commentContent, username:user.username, userPic : user.avatarUrl})

      this.setData({
        commentContent:null,
        commentList,
        myContentNum,
      })
      wx.setStorageSync("myContentNum", myContentNum)

    },e=>{

    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})